package com.isman.placeslist.setting;

import com.facebook.android.Facebook;
import com.isman.placeslist.apiobject.Session;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.location.Location;

public class ProjectPref{
	public static final String WRITE_PRIVATE = "writer_private";
	

	private static final String ID = "id";
    private static final String NAME = "name";
    private static final String EMAIL = "email";
    private static final String TOKEN = "token";
    private static final String TELEPHONE = "telephone";
    private static final String PICTURE = "picture";

    
    private static final String FACEBOOK_TOKEN = "access_token";
    private static final String EXPIRES = "expires_in";
    
	public static final String KEY = "lis-preferences";
	
	private static final String LATITUBE = "Latitude";
	private static final String LONGITUBE = "Longitude";
	
	
	

    
    public static boolean saveSession(Session mSession, Context mContext) {
        Editor editor =
            mContext.getSharedPreferences(KEY, Context.MODE_PRIVATE).edit();
        editor.putLong(ID, mSession.getId());
        editor.putString(NAME, mSession.getName());
        editor.putString(EMAIL, mSession.getEmail());
        editor.putString(TOKEN, mSession.getToken());
        editor.putString(TELEPHONE, mSession.getTelephone());
        editor.putString(PICTURE, mSession.getPicture());
        return editor.commit();
    }
    public static boolean restoreSession(Session mSession, Context mContext) {
        SharedPreferences savedSession =
            mContext.getSharedPreferences(KEY, Context.MODE_PRIVATE);
        mSession.setId(savedSession.getLong(ID, 0));
        mSession.setName(savedSession.getString(NAME, ""));
        mSession.setEmail(savedSession.getString(EMAIL, ""));
        mSession.setToken(savedSession.getString(TOKEN, ""));
        mSession.setTelephone(savedSession.getString(TELEPHONE, ""));
        mSession.setPicture(savedSession.getString(PICTURE, ""));
        
        
        if(mSession.getToken() == "" && mSession.getId() == 0){
        	return false;
        }
        return true;
    }
    
    public static boolean saveLocation(Location mLocation, Context mContext){
    	Editor editor =
                mContext.getSharedPreferences(KEY, Context.MODE_PRIVATE).edit();
    	editor.putString(LATITUBE, String.valueOf(mLocation.getLatitude()));
    	editor.putString(LONGITUBE, String.valueOf(mLocation.getLongitude()));
    	return editor.commit();
    }
    public static boolean restoreLocation(Location mLocation,Context mContext){
    	SharedPreferences savedSession =
                mContext.getSharedPreferences(KEY, Context.MODE_PRIVATE);
    	mLocation.setLatitude(Double.parseDouble(savedSession.getString(LATITUBE, "0")));
    	mLocation.setLongitude(Double.parseDouble(savedSession.getString(LONGITUBE, "0")));
    	if(mLocation.getLatitude() == 0 && mLocation.getLongitude() == 0){
        	return false;
        }
    	return true;
    }
	
//    
//    public static boolean exitAccount(Context context){
//    	SharedPreferences savedSession =
//                context.getSharedPreferences(KEY, Context.MODE_PRIVATE);
//    	if(0 == savedSession.getLong(USER, 0) || null == savedSession.getString(TOKEN, null)){
//    		return false;
//    	}
//    	return true;
//    }
//    public static String restoreEmail(Context context){
//        SharedPreferences savedSession =
//                context.getSharedPreferences(KEY, Context.MODE_PRIVATE);
//        String email = savedSession.getString(EMAIL, "");
//            
//        return email;
//    }
//
//    
    public static boolean saveFbSession(Facebook fb, Context context) {
        Editor editor =
            context.getSharedPreferences(KEY, Context.MODE_PRIVATE).edit();
        editor.putString(FACEBOOK_TOKEN, fb.getAccessToken());
        editor.putLong(EXPIRES, fb.getAccessExpires());
        return editor.commit();
    }
    public static boolean restoreFbSession(Facebook fb, Context context) {
        SharedPreferences savedSession =
            context.getSharedPreferences(KEY, Context.MODE_PRIVATE);
        fb.setAccessToken(savedSession.getString(FACEBOOK_TOKEN, null));
        fb.setAccessExpires(savedSession.getLong(EXPIRES, 0));
        return fb.isSessionValid();
    }
    
    
    public static void clear(Context context) {
        Editor editor = 
        		context.getSharedPreferences(KEY, Context.MODE_PRIVATE).edit();
        editor.clear();
        editor.commit();
	}
}
